package asg.pp;

import java.util.ArrayList;
import java.util.Collections;

import controller.Controller;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Image> defaultList;
	private ArrayList<Image> filterList;
	private PurblePlaceService service;
	
	private int[] id = {
			R.drawable.i01, R.drawable.i02, R.drawable.i03, R.drawable.i04,R.drawable.i05, 
			R.drawable.i06, R.drawable.i07, R.drawable.i08, R.drawable.i09, R.drawable.i10, 
			R.drawable.i11,	R.drawable.i12, R.drawable.i13, R.drawable.i14,	R.drawable.i15,	
			R.drawable.i16, R.drawable.i17, R.drawable.i18, R.drawable.i19, R.drawable.i20
	};

	public ImageAdapter(Context c, ArrayList<Image> list) {
		context = c;
		filterList = list;
		service = PurblePlaceService.getInstance();

		defaultList = new ArrayList<Image>();
		for(int i = 1; i <= 6; i++)
			for(int j = 1; j <= 6; j++) {
				Image defaultImage = new Image(id[i], Image.DEFAULT);
				defaultList.add(defaultImage);
			}
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return filterList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return filterList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView imageView;
		
		if(convertView == null) {
			imageView = new ImageView(context);
			imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setImageResource(filterList.get(position).getId());
			imageView.setId(position);
		}
		else
			imageView = (ImageView) convertView;
		
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i("onclick", "image adapter: " + v.getId());
				service.onClick(v.getId());
			}
		});
		
		return imageView;
	}

}
